/**
 * Copyright © 1998-2020, Glodon Inc. All Rights Reserved.
 */
package reactor2;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 多路复用核心类
 * <p>
 * 此处填写类说明
 * </p>
 * 
 * @author dujl
 * @since jdk1.6 2020年8月6日
 */

public class Selectlor implements Runnable {

	private Demultiplexer<?> delexer = null;
	private EventDispatcher dispatcher = null;
	private ThreadPoolExecutor executor = null;
	int i = 1;

	public Selectlor(Demultiplexer<?> delexer, EventDispatcher dispatcher, ThreadPoolExecutor executor) {
		this.delexer = delexer;
		this.dispatcher = dispatcher;
		this.executor = executor;
		// 启动当前线程进行监听
		executor.execute(this);
	}

	/**
	 * @see Runnable#run()
	 */
	@Override
	public void run() {
		System.out.println(">>>>>>>>Selectlor  run >>>>>>>>>>>>");
		// TODO Auto-generated method stub
		while (true) {
			if (delexer.select()) {
				i++;
				System.out.println(">>>>>>>> i>>>>>>>" + i);
				Event event = (Event) delexer.getMsg();
				IEventHandler handler = dispatcher.getEventHandler(event.getEventType());
				handler.handler(event, executor);
			}
		}
	}
}
